package com.speljohan.rsbot.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.speljohan.rsbot.script.Random;

public class RandomSelector extends JDialog implements ActionListener {
	private final Logger log = Logger.getLogger(RandomSelector.class.getName());

	private static final long serialVersionUID = -3060364633238608218L;

	private final Collection<Random> randoms;

	public RandomSelector(final Window parent, final Collection<Random> randoms) {
		super(parent, "Select Randoms", ModalityType.APPLICATION_MODAL);
		this.randoms = randoms;
		initializeGUI();
	}

	public void actionPerformed(final ActionEvent e) {
		final String command = e.getActionCommand().intern();
		if (command == "OK") {
			dispose();
		} else {
			for (final Random r : randoms) {
				if (command.equals(r.getName())) {
					r.isUsed = !r.isUsed;
					log.info("AntiRandomSelect: " + "Turned "
							+ (r.isUsed ? "On" : "Off") + " " + r.getName());
				}
			}
		}
	}

	public void initializeGUI() {
		int i = 0;
		final JCheckBox[] checkBoxArray = new JCheckBox[randoms.size()];
		final JLabel[] versionLabelArray = new JLabel[randoms.size()];
		final JLabel[] authorLabelArray = new JLabel[randoms.size()];

		final JPanel checkBoxPanel = new JPanel(new BorderLayout());
		final JPanel versionPanel = new JPanel();
		final JPanel authorPanel = new JPanel();

		final JPanel finalPanel = new JPanel();

		checkBoxPanel.setLayout(new BoxLayout(checkBoxPanel,
				BoxLayout.PAGE_AXIS));
		versionPanel
		.setLayout(new BoxLayout(versionPanel, BoxLayout.PAGE_AXIS));
		authorPanel.setLayout(new BoxLayout(authorPanel, BoxLayout.PAGE_AXIS));

		final JLabel randomLabel = new JLabel("Name");
		final JLabel versionLabel = new JLabel("Version");
		final JLabel authorLabel = new JLabel("Author");

		checkBoxPanel.add(randomLabel);
		versionPanel.add(versionLabel);
		authorPanel.add(authorLabel);

		versionPanel.add(Box.createRigidArea(new Dimension(0, 2)));
		authorPanel.add(Box.createRigidArea(new Dimension(0, 2)));
		for (final Random r : randoms) {
			checkBoxArray[i] = new JCheckBox(r.getName());
			checkBoxArray[i].setSelected(r.isUsed);
			checkBoxArray[i].addActionListener(this);

			versionLabelArray[i] = new JLabel("" + r.getVersion());
			authorLabelArray[i] = new JLabel(r.getAuthor());

			checkBoxArray[i].setBorder(BorderFactory.createLineBorder(
					Color.BLACK, 1));

			checkBoxPanel.add(checkBoxArray[i]);
			versionPanel.add(versionLabelArray[i]);
			authorPanel.add(authorLabelArray[i]);

			versionPanel.add(Box.createRigidArea(new Dimension(0, 2)));
			authorPanel.add(Box.createRigidArea(new Dimension(0, 2)));

			i++;
		}

		finalPanel.add(checkBoxPanel);
		finalPanel.add(versionPanel);
		finalPanel.add(authorPanel);

		final JScrollPane panelScroller = new JScrollPane(finalPanel);
		panelScroller.setBorder(null);

		add(panelScroller);
		pack();
		setLocationRelativeTo(getOwner());
	}
}
